/**
 * @project name:crm
 */
package com.yunbo.crm.controller;

import com.yunbo.crm.base.BaseController;
import com.yunbo.crm.base.ResultInfo;
import com.yunbo.crm.pojo.Role;
import com.yunbo.crm.query.RoleQuery;
import com.yunbo.crm.service.RoleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/26 16:24
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    @GetMapping("index")
    public ModelAndView goRoleIndex(ModelAndView modelAndView) {
        modelAndView.setViewName("role/role");
        return modelAndView;
    }

    @GetMapping("toAddGrantPage")
    public ModelAndView toAddGrantPage(ModelAndView modelAndView, Integer rowId) {
        modelAndView.setViewName("role/grant");
        modelAndView.addObject("roleId", rowId);
        return modelAndView;
    }

    @PostMapping("addGrant")
    public ResultInfo addGrant(Integer[] mids, Integer roleId) {
        roleService.addGrant(mids,roleId);
        return success("角色授权成功");
    }

    @GetMapping("list")
    public Map<String, Object> userList(RoleQuery RoleQuery) {
        return roleService.queryByParamsForTable(RoleQuery);
    }

    @GetMapping("addOrUpdateRolePage")
    public ModelAndView addOrUpdateRolePage(ModelAndView modelAndView, Integer rowId) {
        if (null != rowId) {
            modelAndView.addObject("role", roleService.selectByPrimaryKey(rowId));
        }
        modelAndView.setViewName("role/add_update");
        return modelAndView;
    }

    @PostMapping("save")
    public ResultInfo saveRole(Role role, HttpServletRequest request) {
        roleService.saveRole(role, request);
        return success("添加角色成功");
    }

    @PutMapping("update")
    public ResultInfo updateRole(Role role, HttpServletRequest request) {
        roleService.updateRole(role, request);
        return success("角色更新成功");
    }

    @DeleteMapping("delete")
    public ResultInfo deleteRole(Integer rowId, HttpServletRequest request) {
        roleService.deleteRole(rowId, request);
        return success("角色记录删除成功");
    }
}
